package com.hpeng.search.action;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.hpeng.daily.service.AssetCardManager;
import com.hpeng.search.dao.SearchAssetcardbookDao;
import com.hpeng.search.dao.SearchCategoryDao;
import com.hpeng.search.dao.SearchDao;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.model.AssetCardBook;
import com.hpeng.sys.model.Category;
import com.hpeng.sys.model.Department;
import com.hpeng.sys.service.Function;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;

@Component("userDefinedSearchAction")
@Scope("prototype")
public class UserDefinedSearchAction {
	private static final long serialVersionUID = 1L;
	// 用于分页显示
	private int page;
	public String pageLink;
	private Pagination<AssetCard> pagination;

	@Resource
	private AssetCardManager assetCardManager;
	@Resource
	private SearchDao searchDao;
	@Resource
	private SearchAssetcardbookDao searchAssetcardbookDao;
	@Resource
	private SearchCategoryDao searchCategoryDao;
	private List<Department> departments;
	private AssetCard assetCard;

	// 查询值
	private String declarationDepartment;// 申请部门
	private String employDepartment;// 使用部门
	private String user;// 使用人
	private String accountSubject;// 会计科目
	private String orderId;// 凭证编号
	private String category;// 资产类别
	private String assetStatus;// 资产状态
	private String timeScope1, timeScope2;// 时间范围
	private ArrayList<String> queryContinon;
	private String querySql;

	private List<AssetCard> assetCards;// 用于页面上显示资产的状态的list
	private List<AssetCardBook> assetCardBookList;
	private List<Category> categoryList;
	// 用于根据权限生成功能按钮
	@Resource
	private Function function;
	private String accessRight;
	private String FN;
	private boolean flag = false;

	public String queryCard() {

		for (int i = 0; i < this.gotContion().size(); i++) {
			if (i > 0) {
				this.querySql = this.querySql + " and "
				+ this.getQueryContinon().get(i);
			} else {
				this.querySql = "from AssetCard where "
					+ this.getQueryContinon().get(i);
			}
		}
		return querySql;
	}

	public ArrayList<String> gotContion() {
		queryContinon = new ArrayList<String>();
		if (!declarationDepartment.equals("")) {
			queryContinon.add("reportdepartment=" + "'" + declarationDepartment
					+ "' or reportdepartment like '%" + declarationDepartment
					+ "%'");
		}
		if (!employDepartment.equals("")) {
			queryContinon.add("depname=" + "'" + employDepartment
					+ "' or depname like '%" + employDepartment + "%'");
		}
		if (!user.equals("")) {
			queryContinon.add("acuser=" + "'" + user + "' or acuser like '%"
					+ user + "%'");
		}
		if (!accountSubject.equals("")) {
			queryContinon.add("accountsubject=" + "'" + accountSubject
					+ "' or accountsubject like '%" + accountSubject + "%'");
		}
		if (!orderId.equals("")) {
			queryContinon.add("orderid=" + "'" + orderId
					+ "' or orderid like '%" + orderId + "%'");
		}
		if (!assetStatus.equals("")) {
			queryContinon.add("acstatus=" + "'" + assetStatus
					+ "' or acstatus like '%" + assetStatus + "%'");
		}

		return queryContinon;
	}

	public String queryAssetCard() {

		try {
			if (!category.equals("")) {
				categoryList = searchCategoryDao
				.getByHql("from Category where catname='" + category
						+ "' or catname like'%" + category + "'");

				System.out.println("categoryList.size()--------"
						+ categoryList.size());
			}

			if (!timeScope1.equals("") && !timeScope2.equals("")) {

				if (Date.valueOf(timeScope1).before(Date.valueOf(timeScope2))) {
					assetCardBookList = searchAssetcardbookDao
					.getByHql("from AssetCardBook"
							+ " where operatedate>" + "to_date('"
							+ timeScope1
							+ "','yyyy-mm-dd') and operatedate<"
							+ "to_date('" + timeScope2
							+ "','yyyy-mm-dd')");
				} else if (Date.valueOf(timeScope1).equals(
						Date.valueOf(timeScope2))) {
					assetCardBookList = searchAssetcardbookDao
					.getByHql("from AssetCardBook"
							+ " where operatedate=" + "to_date('"
							+ timeScope1 + "','yyyy-mm-dd')");
				} else {
					assetCardBookList = searchAssetcardbookDao
					.getByHql("from AssetCardBook"
							+ " where operatedate<" + "to_date('"
							+ timeScope1 + "','yyyy-mm-dd')"
							+ "and operatedate>" + "to_date('"
							+ timeScope1 + "','yyyy-mm-dd')");
				}
			}
			this.queryCard();
			System.out.println("this.queryCard();----------"+this.queryCard());
			if (querySql != null) {
				List<AssetCard> a = searchDao.getByHql(this.queryCard());
				System.out.println("a-----------"+a.size());
				List<AssetCard> b = new ArrayList<AssetCard>();
				List<AssetCard> c = new ArrayList<AssetCard>();
				if (!category.equals("") && categoryList.size() > 0) {
					b = searchDao.getAll();
					for (int i = 0; i < categoryList.size(); i++) {

						for (int j = 0; j < b.size(); j++) {
							if (categoryList.get(i).equals(
									b.get(j).getCategory())) {
								c.add(b.get(j));
							}

						}
					}
					for (int i = 0; i < a.size(); i++) {
						boolean flag = false;
						for (int j = 0; j < c.size(); j++) {

							if (a.get(i).getAcid().equals(c.get(j).getAcid())) {
								flag = true;
							}
						}
						if (!flag) {
							a.remove(i);
							i--;
						}
					}
				}
				if (!timeScope1.equals("") && !timeScope2.equals("")
						&& assetCardBookList.size() > 0) {
					for (int i = 0; i < a.size(); i++) {
						boolean flag = false;
						for (int j = 0; j < assetCardBookList.size(); j++) {
							if(assetCardBookList.get(j).getCard().getAcid()==null){
								assetCardBookList.remove(j);
								j--;
							}
							if (a.get(i).getAcid().equals(
									assetCardBookList.get(j).getCard()
									.getAcid())) {
								flag = true;
							}
						}
						if (!flag) {
							a.remove(i);
							i--;
						}
					}

				}
				if (a.size() > 0) {
					System.out.println("go   into!!!!");
					assetCards = a;
				} else {
					// 无查询结果
				}

			} else {
				List<AssetCard> a = new ArrayList<AssetCard>();
				List<AssetCard> b = new ArrayList<AssetCard>();
				List<AssetCard> c = new ArrayList<AssetCard>();
				if (!category.equals("") && !timeScope1.equals("")
						&& !timeScope2.equals("")) {
					if (assetCardBookList.size() > 0 && categoryList.size() > 0) {
						b = searchDao.getAll();
						for (int i = 0; i < categoryList.size(); i++) {

							for (int j = 0; j < b.size(); j++) {
								if (categoryList.get(i).equals(
										b.get(j).getCategory())) {
									c.add(b.get(j));
								}

							}
						}
						for (int i = 0; i < assetCardBookList.size(); i++) {
							System.out.println(assetCardBookList.get(i).getCard()==null);
							if(assetCardBookList.get(i).getCard()!=null){
								a.add(assetCardBookList.get(i).getCard());
							}
						}
						int count=0;
						for (int i = 0; i < a.size(); i++) {
							flag = false;
							for (int j = 0; j < c.size(); j++) {
								if (a.get(i).getAcid().equals(
										c.get(j).getAcid())) {
									flag = true;
									break;
								}
							}
							if (!flag) {
								count++;
								a.remove(i);
								i--;
							}

						}
						assetCards = a;
					} else if (categoryList.size() > 0
							&& assetCardBookList.size() == 0) {
						b = searchDao.getAll();
						for (int i = 0; i < categoryList.size(); i++) {

							for (int j = 0; i < b.size(); j++) {
								if (categoryList.get(i).equals(
										b.get(j).getCategory())) {
									c.add(b.get(j));
								}

							}
						}
						assetCards = c;
					} else if (categoryList.size() == 0
							&& assetCardBookList.size() > 0) {
						for (int i = 0; i < assetCardBookList.size(); i++) {
							if(assetCardBookList.get(i).getCard()!=null){
								a.add(assetCardBookList.get(i).getCard());
							}

						}
						for (int i = 0; i < a.size(); i++) {
							boolean flag = false;
							for (int j = 0; j < c.size(); j++) {
								if (a.get(i).getAcid().equals(
										c.get(j).getAcid())) {
									flag = true;
								}
							}
							if (!flag) {
								a.remove(i);
								i--;
							}

						}
						assetCards = a;
					} else {
						// 无查询记录
					}

				} else if (!category.equals("") && categoryList.size() > 0) {
					assetCards = new ArrayList<AssetCard>();
					b = searchDao.getAll();
					for (int i = 0; i < categoryList.size(); i++) {

						for (int j = 0; j < b.size(); j++) {
							if (categoryList.get(i).equals(
									b.get(j).getCategory())) {
								assetCards.add(b.get(j));
							}

						}
					}
				} else if (!timeScope1.equals("") && !timeScope2.equals("")
						&& assetCardBookList.size() > 0) {
					for (int i = 0; i < assetCardBookList.size(); i++) {
						if(assetCardBookList.get(i).getCard()!=null){
							b.add(assetCardBookList.get(i).getCard());
						}

					}
					assetCards = b;
				} else {
					// 无查询记录
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	//	HttpServletRequest request = ServletActionContext.getRequest();
		// 只能查询出登录员所在的部门处于在用和进入的盘点资产
		String str=new String();
//<<<<<<< .mine
//		try {
//		for (int i = 0; i < assetCards.size(); i++) {
//			if(i>0){
//			str+=" or acid='"+assetCards.get(i).getAcid()+"'";
//			}else{
//				str="acid='"+assetCards.get(i).getAcid()+"'";
//=======
		System.out.println("assetCards.size()---------"+assetCards.size());
		if(assetCards.size()>0){
			for (int i = 0; i < assetCards.size(); i++) {
				if(i>0){
					str+=" or acid='"+assetCards.get(i).getAcid()+"'";
				}else{
					str="acid='"+assetCards.get(i).getAcid()+"'";
				}
//>>>>>>> .r973
			}
//<<<<<<< .mine
	//	}

	//	} catch (Exception e) {
		//	e.printStackTrace();
	//	}

	//	String[] hql = { " from AssetCard where "+ str };
//=======
			String[] hql = { " from AssetCard where "
					+ str };
//>>>>>>> .r973
			String order = " order by acid asc ";
			setPagination(searchDao.getPagination(page, hql, order));
			pageLink = pagination.getPageLink("userDefinedSearch.action?FN="+FN+"&declarationDepartment="+declarationDepartment
					+"&employDepartment="+employDepartment+"&user="+user+"&accountSubject="+accountSubject
					+"&orderId="+orderId+"&category="+category+"&assetStatus="+assetStatus+"&timeScope1="+timeScope1
					+"&timeScope2="+timeScope2);

			this.accessRight = function.getAccessRight(session(), FN);
		}else{
			System.out.println("无查询记录");
		}

		return "success";
	}








	/**************** set get **********************/

	public Map<String, Object> session() {
		return ActionContext.getContext().getSession();
	}

	public HttpServletRequest request() {
		return ServletActionContext.getRequest();
	}

	public List<AssetCard> getAssetCards() {
		return assetCards;
	}

	public void setAssetCards(List<AssetCard> assetCards) {
		this.assetCards = assetCards;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getPageLink() {
		return pageLink;
	}

	public void setPageLink(String pageLink) {
		this.pageLink = pageLink;
	}

	public Pagination<AssetCard> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<AssetCard> pagination) {
		this.pagination = pagination;
	}

	public AssetCardManager getAssetCardManager() {
		return assetCardManager;
	}

	public void setAssetCardManager(AssetCardManager assetCardManager) {
		this.assetCardManager = assetCardManager;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public AssetCard getAssetCard() {
		return assetCard;
	}

	public void setAssetCard(AssetCard assetCard) {
		this.assetCard = assetCard;
	}

	public String getDeclarationDepartment() {
		return declarationDepartment;
	}

	public void setDeclarationDepartment(String declarationDepartment) {
		this.declarationDepartment = declarationDepartment;
	}

	public String getEmployDepartment() {
		return employDepartment;
	}

	public void setEmployDepartment(String employDepartment) {
		this.employDepartment = employDepartment;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getAccountSubject() {
		return accountSubject;
	}

	public void setAccountSubject(String accountSubject) {
		this.accountSubject = accountSubject;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getAssetStatus() {
		return assetStatus;
	}

	public void setAssetStatus(String assetStatus) {
		this.assetStatus = assetStatus;
	}

	public String getTimeScope1() {
		return timeScope1;
	}

	public void setTimeScope1(String timeScope1) {
		this.timeScope1 = timeScope1;
	}

	public String getTimeScope2() {
		return timeScope2;
	}

	public void setTimeScope2(String timeScope2) {
		this.timeScope2 = timeScope2;
	}

	public ArrayList<String> getQueryContinon() {
		return queryContinon;
	}

	public void setQueryContinon(ArrayList<String> queryContinon) {
		this.queryContinon = queryContinon;
	}

	public String getQuerySql() {
		return querySql;
	}

	public void setQuerySql(String querySql) {
		this.querySql = querySql;
	}

	public Function getFunction() {
		return function;
	}

	public void setFunction(Function function) {
		this.function = function;
	}

	public String getAccessRight() {
		return accessRight;
	}

	public void setAccessRight(String accessRight) {
		this.accessRight = accessRight;
	}

	public String getFN() {
		return FN;
	}

	public void setFN(String fN) {
		FN = fN;
	}
}
